package com.ruoyi.plus.pub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.plus.pub.domain.PubResources;
import com.ruoyi.plus.pub.domain.PubResourcesClass;
import com.ruoyi.plus.pub.service.IPubResourcesClassService;
import com.ruoyi.plus.pub.service.IPubResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源分类管理Controller
 */
@RestController
@RequestMapping("/pub/resourcesClass")
public class PubResourcesClassController extends BaseController {
    @Autowired
    private IPubResourcesClassService pubResourcesClassService;

    @Autowired
    private IPubResourcesService pubResourcesService;


    /**
     * 查询资源分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('pub:resourcesClass:list')")
    @GetMapping("/list")
    public AjaxResult list(PubResourcesClass pubResourcesClass) {
        List<PubResourcesClass> list = pubResourcesClassService.selectPubResourcesClassList(pubResourcesClass);
        return AjaxResult.success(list);
    }

    /**
     * 导出资源分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('pub:resourcesClass:export')")
    @Log(title = "资源分类管理导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PubResourcesClass pubResourcesClass) {
        List<PubResourcesClass> list = pubResourcesClassService.selectPubResourcesClassList(pubResourcesClass);
        ExcelUtil<PubResourcesClass> util = new ExcelUtil<>(PubResourcesClass.class);
        return util.exportExcel(list, "resourcesClass");
    }

    /**
     * 获取资源分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pub:resourcesClass:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pubResourcesClassService.getById(id));
    }

    /**
     * 新增资源分类管理
     */
    @PreAuthorize("@ss.hasPermi('pub:resourcesClass:add')")
    @Log(title = "资源分类管理添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PubResourcesClass pubResourcesClass) {
        return pubResourcesClassService.addResourcesClass(pubResourcesClass);
    }

    /**
     * 修改资源分类管理
     */
    @PreAuthorize("@ss.hasPermi('pub:resourcesClass:edit')")
    @Log(title = "资源分类管理修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PubResourcesClass pubResourcesClass) {
        return pubResourcesClassService.updateResourcesClass(pubResourcesClass);
    }

    /**
     * 删除资源分类管理
     */
    @PreAuthorize("@ss.hasPermi('pub:resourcesClass:remove')")
    @Log(title = "资源分类管理删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        long count = 0;
        // 校验分类下是否存在资源
        QueryWrapper<PubResources> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resources_class_id", id);
        count = pubResourcesService.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error("该分类下存在资源，不能删除！");
        }
        return pubResourcesClassService.deleteResourcesClass(id);
    }

    /**
     * 获取模板分类下拉树列表
     */
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect(PubResourcesClass pubResourcesClass) {
        List<PubResourcesClass> pubResourcesClasses = pubResourcesClassService.selectPubResourcesClassList(pubResourcesClass);
        return AjaxResult.success(pubResourcesClassService.buildTemplateClassTreeSelect(pubResourcesClasses));
    }

}
